package com.evola.rm.utils
{
	import com.evola.rm.model.Alias;
	import com.evola.rm.model.Language;
	import com.evola.rm.model.Project;
	import com.evola.rm.view.project.ProjectModel;

	import mx.collections.ArrayCollection;

	public class XMLConvertor
	{


		public static function createFromXML(xml:XML):Project
		{

			var project:Project=new Project();
			project.name=xml.project.@name;
			project.languages=new ArrayCollection();
			project.aliases=new ArrayCollection();

			for each (var languageXML:XML in xml.languages.language)
			{

				var language:Language=new Language();
				language.name=languageXML.@name;
				language.code=languageXML.@code;
				language.path=languageXML.@file;

				project.languages.addItem(language);
			}

			for each (var aliasXML:XML in xml.aliases.alias)
			{

				var alias:Alias=new Alias();
				alias.name=aliasXML.@name;
				alias.code=aliasXML.@code;

				if (aliasXML.hasOwnProperty("@color"))
					alias.color=uint(aliasXML.@color);

				project.aliases.addItem(alias);
			}

			return project;
		}

		public static function exportToXml(project:Project):XML
		{

			var xml:XML=<evola-rm>
					<project name={project.name}/>
					<languages/>
					<aliases/>
				</evola-rm>;

			for each (var lang:Language in project.languages)
			{
				xml.languages.appendChild(<language code={lang.code} name={lang.name} file={lang.path}/>);
			}

			for each (var alias:Alias in project.aliases)
			{
				var aliasXML:XML=<alias code={alias.code} name={alias.name}/>;

				if (alias.color)
					aliasXML.@color="0x" + alias.color.toString(16);

				xml.aliases.appendChild(aliasXML);
			}

			return xml;
		}
	}
}
